[#ftl]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>[#assign module="authorities"][#assign moduleName="授权"]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="${base}/commonjs/flexgrid/css/flexigrid.pack.css" />

<script type="text/javascript" src="${base}/commonjs/jquery1.7.2/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="${base}/commonjs/flexgrid/js/flexigrid.js"></script>
<script type="text/javascript" src="${base}/commonjs/flexgrid/js/myflexigrid.js"></script>
<script type="text/javascript" src="${base}/commonjs/artDialog4/artDialog.source.js?skin=blue"></script>
<script type="text/javascript" src="${base}/commonjs/artDialog4/plugins/iframeTools.source.js"></script>
<script type="text/javascript" src="${base}/commonjs/jquery.itembox.js"></script>
<script type="text/javascript">
function addToAuthorities(checkbox, id, text){
    if(checkbox.checked){
        $("#divAuthorityItemBox").addItemBox({id: id, name: text});
    }else{
        $("#divAuthorityItemBox").removeItemBox(id);
    }
}
function showResult(){
    var items = $("#divAuthorityItemBox").getItemBoxes();
    var sb = [];
    for(var i=0; i<items.length; i++){
        var item = items[i];
        sb.push("{id:", item.id, ", name: '", item.name, "', persist: ", !!item.persist, ", del: ", !!item.del, "}\n");
    }
    alert(sb.join(""));   
}

function $1(id){
    return  $("#"+id).val()
}
function flexGridReload(selector){
    $(selector).flexReload();
}
function isAuthorityChecked(id){
    return $("#divAuthorityItemBox").isItemBoxSelected(id);
}
//关系添加、删除、恢复的时候会触发此动作
//添加的时候，data即为添加的对象，persist不论，del为空
//删除的时候“[X]”，del赋值为true，persist不论。
//恢复的时候“[X]”，del赋值为false， persist为true
function itemBoxDeleteEventCallBack(data){
    if(!!data.persist){//原先有的对象，不用处理
        //确认一下UI
        var checkbox = $("#auth_"+data.id);
        if(checkbox.length==1){
            checkbox.attr("checked", !data.del);
        }
    }else
    if(!!data.del){//删除该对象。这里不删除，标记一下即可。
        //确认一下UI
        var checkbox = $("#auth_"+data.id);
        if(checkbox.length==1){
            checkbox.attr("checked", false);
        }
    }
}
var grid;
$(function(){
    var box = $("#divAuthorityItemBox")
    var authorities = [[#list authorities as auth]{id: ${auth.id}, name: "${auth.name}(${auth.authority})", persist: true}[#if auth_has_next], 
                 [/#if][/#list]];
    box.addItemBox.apply(box, authorities);

    grid = $(".flexme3").flexigrid(generateFlexGridCfg({
        title:'${moduleName}列表',
        url : '${base}/authorities/list',
        dataType : 'json',
        singleSelect: true,
        colModel:[
            ["ID", "id", 40, true, "asc",  "left", null],
            ["Name", "name", 80, true, "",  "left", null],
            ["Authority", "authority", 80, true, "",  "left", null],
            ["Description", "description", 100, false, "",  "left", null],
            ["选择", "description", 30, false, "",  "left", function(data, id, rowData){
                var sb = ["<input type='checkbox' id='auth_",
                          id,
                          "' value='",
                          id,
                          "' onclick='addToAuthorities(this, ",
                          id,
                          ", \"",
                          rowData[1],
                          "(",
                          rowData[2],
                          ")\")'"];
                          
                if(isAuthorityChecked(id)){
                    sb.push(" checked");
                }
                sb.push(">");
                return sb.join("")
            }]
        ],
        pageSize: 1,
        height: 150
    }));
    $("#btOK").click(function(){
        var items = $("#divAuthorityItemBox").getItemBoxes();
        var content = [];
        var ids = [];
        $(items).each(function(i, item){
            if(!!item.persist){
                if(!!item.del){
                    ids.push(-item.id);
                    content.push("删除【"+item.name+"】");
                }
            }else{
                ids.push(item.id);
                content.push("添加【"+item.name+"】");
            }
        });
        if(ids.length==0){
            art.dialog.through({
                title: "未保存${moduleName}",
                icon: "warning",
                content: "${moduleName}未改变，未提交保存命令！",
                width: 250,
                height: 100,
                background: '#EEE', // 背景色
                opacity: 0.05,  // 透明度
                lock: true,
                ok: true
            });
        }else{
            art.dialog.confirm(content.join("<br>"), function(){
                $.post("${base}/clientapps/${clientAppId}/authorities/edit", {
                    resourceIds: ids.join(",")
                }, function(result){
                    if(result.success){
                        art.dialog.through({
                            title: "保存${moduleName}成功",
                            icon: "succeed",
                            content: content.join(", "),
                            width: 250,
                            height: 100,
                            background: '#EEE', // 背景色
                            opacity: 0.05,  // 透明度
                            lock: true,
                            ok: function(){
                                art.dialog.opener.window.flexGridReload(".flexme3");
                                art.dialog.close();
                            }
                        });
                    }else{
                        art.dialog.through({
                            title: "保存${moduleName}失败",
                            icon: "error",
                            content: result.err,
                            width: 250,
                            height: 100,
                            background: '#EEE', // 背景色
                            opacity: 0.05,  // 透明度
                            lock: true,
                            ok: true
                        });
                    }
                });
            
            }, function(){
                
            });
        }
    });
    $("#btCancel").click(function(){
        art.dialog.close();
    });
});
</script>
<title>${moduleName}列表</title>
</head>
<body>
<h3>[#if clientAppId>0]编辑应用的访问资源[#else]创建应用的访问资源[/#if]</h3>
<div id="divAuthorityItemBox" style="clear: both"></div>
<div style="clear: both"><table class="flexme3" style="display: none"></table></div>
<div><input type="button" id="btOK" value="确定"><input type="button" id="btCancel" value="取消"></div>
<!--input type="button" value="Resources" onclick="showResult()"-->

</body>
</html>